package com.heima.article.lisener;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class UpDownListener {

    @Autowired
    private IApArticleService apArticleService;

    @KafkaListener(topics = "${topic.upDownTopic}")
    public void upDownTopicLisener(ConsumerRecord<String,String> record){
        String key = record.key();
        long articleId = Long.parseLong(key);
        String value = record.value();
        Integer isDown = value.equals("1")?0:1;
        LambdaUpdateWrapper<ApArticle> update = new LambdaUpdateWrapper<>();
        update.eq(ApArticle::getAuthorId,articleId);
        update.set(ApArticle::getIsDown,isDown);
        apArticleService.update(update);
    }
}
